/*$(function() {*/
	//微信初始化和分享处理
    var mywechat={
    	appId:null,
    	timestamp:null,
    	nonceStr:null,
    	signature:null,
    	share_url:null,
    	share_img:null,//share_img是一个完整的图片地址,由action初始化或图片上传之后重置,不包括被处理之后x-oss-process,注意区分上传的share_img只是一个img名称;
    	afterShare:function(type){
    		//搜集用户分享给朋友和分享到朋友圈次数
    		$.ajax({
		         type: "POST",
		         data:{act_id:act_id,type:type},//,share_title:$.trim($('#share_title').val()),share_content:$.trim($('#share_desc').val()),share_img:mywechat.share_img
		         url: "/wechat/collect_share",
		         dataType: "json",
		         success: function(data){
			         if(data.status){
			        	//分享成功
			        	
			         }else{
			        	 
			         }
		         }
		    });
    	},
    	ini_wechat_share:function(){
    		if(mywechat.appId == null){
    			mywechat.share_url = share_url;//window.location.href;//不一定是自己链接
        		
        		$.ajax({
    		         type: "POST",
    		         data:{url:mywechat.share_url},
    		         url: "/wechat/ini_wechat_share",
    		         dataType: "json",
    		         success: function(data){
    			         if(data.status){
    			        	//设置参数
    			        	 mywechat.appId = data.data.appid;
    			        	 mywechat.timestamp = data.data.timestamp;
    			        	 mywechat.nonceStr = data.data.noncestr;
    			        	 mywechat.signature = data.data.signature;
    			        	 mywechat.ini_config();
    			         }else{
    			        	 common.alert(data.msg);
    			         }
    		         }
    		    });
    		}
    	},
    	ini_config:function(){
    		 wx.config({
    		        //debug : true, 
    		        appId : mywechat.appId, 
    		        timestamp : mywechat.timestamp, 
    		        nonceStr  : mywechat.nonceStr, 
    		        signature : mywechat.signature,
    		        jsApiList : [ 'hideMenuItems','onMenuShareAppMessage','onMenuShareTimeline','chooseWXPay']
    		 });
    		 
    		 //预览模式增加底部返回编辑大按钮;
    		 if(window.location.href.indexOf("#preview")>0){
    			 //temp.closeme();//先关闭按钮
    			 foot_style='width: 100%; height:60px; vertical-align: middle; text-align: center; background:#fff; position:fixed; bottom:0;';
    			 a_style ='float:left;border: 1px solid #455571;border-radius: 5px;display: block;font-size: 16px;height: 40px;letter-spacing: 2px;line-height: 40px;margin: 10px;text-align: center;';
    			 
    			 href = 'http://'+location.host+'/act/'+act_id+'/0/#sharenow'; //uid在活动页面得到
    			 html ='<a href="/template/edit/0/'+act_id+'" style="'+a_style+'width:43%;color:#666;">继续编辑活动</a>'+
    			 '<a style="'+a_style+'color:#fff;width:40%;margin-right:10px;background:#455571 none repeat scroll 0 0" href="'+href+'">发布活动</a>';
    			 
    			 $('.bmb').css('height','90px');
    			 $('.footer').attr('style',foot_style);
    			 $('.footer').html(html);
    		 }else if(document.referrer.indexOf("/template/edit/")>0 || window.location.href.indexOf("sharenow")>0){
    			 //只有发布者刚编辑或发布才可以进行分享设置|增加用户从分享页跳转之后立即分享
    			 html = '<div class="szfx">'+
    				    	'<div class="tc">'+
    				 			'<p>分享后朋友们看见的标题、图标和描述.</p>'+
    				 			'<p><input id="share_title" value="'+share_title+'"></p>'+
    				 			'<div>'+
    				 				'<div class="ghtb" id="h5sharecontainer"><img id="resetimg"  src="'+share_img+'">'+
    				 					'<span id="sharebutton"><a  href="javascript:;">更换图标</a></span>'+
    				 				'</div>'+
    				 				'<div class="szms">'+
    				 					'<textarea id="share_desc">'+share_content+'</textarea>'+
    				 				'</div>'+
    				 			'</div>'+
    				 			'<div class="clear"></div>'+
    				 			'<div class="fx"><a href="javascript:;" id="noshare">暂不分享</a><a class="ljfx" href="javascript:;" id="yesshare">保存并分享</a></div>'+
    				 			'<div class="clear"></div>'+
    				 		'</div>'+
    				 	'</div>';
    			 $('.append').append(html);
    			 //增加需要上传oss的地址
        		 $('.append').append('<script src="/js/oss_upload/plupload.full.min.js"></script><script src="/js/oss_upload/wechatup.js?2"></script>');
    		 }else{
    			 //用户直接初始化的分享
    			 wx.ready(function(){
    				 if(share_title==null){
    					 share_title=$("title").html();
    				 }
    				 if(share_content==null){
    					 share_content=$("title").html();
    				 }
    				 mywechat.share_img = share_img;//初始化
    				 
	    			 wx.onMenuShareAppMessage({
		    	        title: share_title,	//如果作者已经存在分享，则存在分享标题内容，否则均使用title
		    	        desc:  share_content,
		    	        link:  mywechat.share_url,
		    	        imgUrl: mywechat.share_img,
		    	        success: function (res) {
		    	        	mywechat.afterShare('timeline');
		    	        },
		    	        cancel: function (res) {
		    	        },
		    	        fail: function (res) {
		    	        	common.alert('分享不成功，请稍后再试.');
		    	        }
		    	     });
		    	    
		    	    // 分享到朋友圈
		    	    wx.onMenuShareTimeline({
		    	    	title: share_title,
		    	        link: mywechat.share_url, // 分享链接
		    	        imgUrl: mywechat.share_img, // 分享图标
		    	        success: function () { 
		    	        	mywechat.afterShare('friend');
		    	        },
		    	        cancel: function () { 
		    	        }
		    	    });
    			 });
    		 }
    		 
    		 //微信事件注册
    		 wx.ready(function(){
		        // 分享事件必须放在这里
		    	wx.hideMenuItems({
		    		menuList:['menuItem:copyUrl',
					              'menuItem:openWithQQBrowser',
					              'menuItem:openWithSafari',
					              'menuItem:share:qq',
					              'menuItem:share:QZone',
					              'menuItem:share:weiboApp',
					              'menuItem:share:facebook']
		    	});
		    });
    	},
    	//上传图片名称保存，避免丢失索引无法使用
    	share_save:function(share_new_img_name){
    		//上传分享数据
    		mywechat.share_img = img_host+'/'+share_new_img_name+'?x-oss-process=style/kn200';//重置分享的图片物理地址
    		
    		$.ajax({
    	         type: "POST",
    	         data:{act_id:act_id,share_img:share_new_img_name},//share_new_img_name包括路径,在上传之后就更新，分享按钮确认之后保存分享的内容.注意区分
    	         url: "/template/share_save/"+act_id,
    	         dataType: "json",
    	         success: function(data){
    	         }
    	    });
    	}
    	
    }
    
    $('.append').on("click",'#noshare',function(){
    	$('.szfx').hide();
    	$.ajax({
	         type: "POST",
	         data:{act_id:act_id,share_title:$.trim($('#share_title').val()),share_content:$.trim($('#share_desc').val())},//share_img在上传完成时就更新一次,这里再次确保更新
	         url: "/template/share_save/"+act_id,
	         dataType: "json",
	         success: function(data){
		         if(data.status){
		         }else{
		         }
	         }
	    });
    });
    
    $('.append').on("click",'#yesshare',function(){
    	$('.szfx').hide();
    	//上传分享数据
    	$.ajax({
	         type: "POST",
	         data:{act_id:act_id,share_title:$.trim($('#share_title').val()),share_content:$.trim($('#share_desc').val())},//share_img在上传完成时就更新一次,这里再次确保更新
	         url: "/template/share_save/"+act_id,
	         dataType: "json",
	         success: function(data){
		         if(data.status){
		         }else{
		         }
	         }
	    });
    	
	    
    	//提示用户使用右上角进行分享
    	wx.ready(function(){
    	    // 分享给朋友
    		//mywechat.share_img = share_img;//千万不能增加，此处是在click之后才生成，mywechat.share_img在此之前已经初始化
    		if(mywechat.share_img==null){
    			mywechat.share_img = share_img;
    		}
    		
    	    wx.onMenuShareAppMessage({
    	        title: $.trim($('#share_title').val()),
    	        desc:  $.trim($('#share_desc').val()),
    	        link:  mywechat.share_url,
    	        imgUrl: mywechat.share_img,
    	        trigger: function (res) {
    	        },
    	        success: function (res) {
    	        	mywechat.afterShare('timeline');
    	        },
    	        cancel: function (res) {
    	        },
    	        fail: function (res) {
    	        	common.alert(JSON.stringify(res));
    	        }
    	     });
    	    
    	    // 分享到朋友圈
    	    wx.onMenuShareTimeline({
    	    	title: $.trim($('#share_title').val()),
    	        link: mywechat.share_url, // 分享链接
    	        imgUrl: mywechat.share_img, // 分享图标
    	        success: function () { 
    	        	mywechat.afterShare('friend');
    	        },
    	        cancel: function () { 
    	        }
    	    });
    	});
    	
    	//弹层提示分享
	    $('.append').html('');
	    $('.append').append('<div class="ydfx"><img class="hideme" src="/img/share.png?1"></div>');
    });

    //页面一加载完成，必须完成微信初始化
    mywechat.ini_wechat_share();
/*});*/

